<template>
  <div class="app-container calendar-list-container">
    <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="订单号" :permissions="permissions" @add="doAdd"></buttons>
    <grid gridTitle="退料单" :url="url" :columns="columns" :enableEdit="false" :loading="true" ref="mainGrid" height="auto" diffHeight="105" :pagination="true"
      :permissions="permissions" :multi="false" @change="rowCilck" :queryEntity="queryEntity" @view="doView" @pass="doPass" @cancel="doCancel" @delete="doDelete">
    </grid>
  </div>
</template>
<script>
import request from '@/router/axios'
import { Message, MessageBox } from 'element-ui'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import { mapGetters } from 'vuex'
import { BusinessOrderStatusFormatter, BusinessTypeFormatter, YesOrNoFormatter } from '@/const/dict'
import { colunms } from '@/const/colunms'
import { dict } from '@/const/dict'
import { queryField } from '@/const/query'

export default {
  components: { grid, buttons },
  data () {
    return {
      orderNo: '',
      url: url.BUSINESS_BACK_LIST_URL,
      entity: {},
      queryEntity: { key: 'key' },
      disabled: true,
      columns: [
        {
          label: '操作', width: 160, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '查看', clickName: 'view', auth: 'business_back_view' },
            { name: '通过', clickName: 'pass', auth: 'business_back_pass', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '取消', clickName: 'cancel', auth: 'business_back_cancel', confirm: true, valueProp: "orderStatus", showValue: ["UN_DO", "FINISH"] },
            { name: '删除', clickName: 'delete', auth: 'business_back_delete', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
          ]
        },
        { label: '退料单号', width: 180, prop: 'backCode', align: 'center' },
        { label: '大货订单', width: 180, prop: 'orderNo', align: 'center' },
        { label: '加工单号', width: 180, prop: 'businessCode', align: 'center' },
        { label: '工序', width: 120, prop: 'stepName', align: 'center' },
        { label: '客户名称', width: 200, prop: 'customerName', align: 'center' },
        { label: '仓库名称', width: 120, prop: 'warehouseName', align: 'center' },
        { label: '卷数', width: 80, prop: 'total', align: 'center' },
        { label: '数量', width: 80, prop: 'quantity', align: 'center' },
        { label: '单价', width: 80, prop: 'price', align: 'center' },
        { label: '订单状态', width: 80, prop: 'orderStatus', align: 'center', formatter: BusinessOrderStatusFormatter },
        { label: '创建人', width: 100, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'business_back_add', ctr: false },
        //  { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'business_back_view', ctr: true },
        //  { name: '通过', type: 'primary', icon: 'edit', click: 'pass', auth: 'business_back_pass', ctr: true, confirm: true },
        //  { name: '取消', type: 'primary', icon: 'edit', click: 'cancel', auth: 'business_back_cancel', ctr: true, confirm: true },
        //  { name: '删除', type: 'primary', icon: 'edit', click: 'delete', auth: 'business_back_delete', ctr: true, confirm: true }
      ]
    }
  },
  created () {
    this.queryEntity.businessCode = this.$route.query.businessCode
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    doView (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({ path: '/prod/business/back/view', query: { businessCode: row.businessCode, backCode: row.backCode, index: true } })
      }
    },
    doAdd () {
      this.$router.push({ path: '/prod/business/back/add', query: { businessCode: this.queryEntity.businessCode, index: true } })
    },
    doCancel (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.cancelOp(row.backCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    cancelOp (backCode) {
      return request({
        url: url.BUSINESS_BACK_CANCEL_URL,
        method: 'post',
        data: { key: 'key' },
        params: { backCode: backCode }
      })
    },
    doDelete (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.deleteOp(row.backCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    deleteOp (backCode) {
      return request({
        url: url.BUSINESS_BACK_DELETE_URL,
        method: 'post',
        data: { key: 'key' },
        params: { backCode: backCode }
      })
    },
    doPass (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.passOp(row.backCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    passOp (backCode) {
      return request({
        url: url.BUSINESS_BACK_PASS_URL,
        method: 'post',
        data: { key: 'key' },
        params: { backCode: backCode }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.w-body {
  padding-top: 8px;
}
.w-table {
  border: 1px solid #ebeef5;
}
.w-table-title {
  border: 1px solid #ebeef5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  line-height: 23px;
  display: inline-block;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: relative;
  vertical-align: middle;
  width: 100%;
  font-size: 14px;
  color: gray;
}
</style>
